package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.UserMsgConstant;
import com.tbit.main.pojo.Suggestion;
import com.tbit.main.pojo.UserMsg;
import com.tbit.main.service.SuggestionService;
import com.tbit.main.service.UserMsgService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户反馈
 *
 * @author Leon
 * 2020年4月9日 下午2:16:27
 */
@RestController
@RequestMapping("/suggestion")
public class SuggestionController {
    @Autowired
    private SuggestionService suggestionService;
    @Autowired
    private UserMsgService userMsgService;

    /**
     * 处理
     */
    @RequestMapping("/handle")
    public JsonResult handle(String token, Suggestion suggestion,
                             @RequestParam(value = "state", required = false) Integer state) {
        /* 校验 */
        Assert.notNull(suggestion.getSuggestionId(), "System.id.notNull");
        Assert.hasLength(suggestion.getReply(), "Suggestion.replay.notNull");

        suggestion.setState(1);
        suggestion.setHandleTime(DateTimeUtil.getNowTime());
        suggestion.setRemark("");

        suggestionService.update(suggestion);
        if (state == null) {
            state = 0;
        }
        /* 添加用户消息*/
        if (state.equals(1)) {
            Suggestion suggestionDB = suggestionService.getBySuggestionId(suggestion.getSuggestionId());
            UserMsg userMsg = new UserMsg();
            userMsg.setMsgType(UserMsgConstant.UserMsg_PERSON);
            userMsg.setUserId(suggestionDB.getUserId());
            userMsg.setCreateTime(DateTimeUtil.getNowTime());
            userMsg.setMsgState(0);
            userMsg.setTitle(suggestionDB.getTitle());
            userMsg.setContent(suggestion.getReply());
            userMsg.setRemark("运维端发送");
            userMsgService.insert(userMsg);
        }

        return JsonResult.succ();
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public JsonResult query(String token, Integer accountId, Integer state, String startTime, String endTime) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<Suggestion> suggestions = suggestionService.query(accountId, state, startTime, endTime);

        return JsonResult.succ(suggestions);
    }

    /**
     * 分页查询
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String token, Integer accountId, Integer state, String startTime, String endTime, Integer rowCount, Integer pageNO) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        if (rowCount == null) {
            rowCount = 10;
        }

        if (pageNO == null) {
            pageNO = 1;
        }

        Integer suggestionsContent = suggestionService.queryContent(accountId, state, startTime, endTime);
        List<Suggestion> suggestions = new ArrayList<>();
        if (suggestionsContent > 0) {
            suggestions = suggestionService.queryPage(accountId, state, startTime, endTime, rowCount, pageNO);
        }

        return JsonResult.succ(suggestionsContent, suggestions);
    }
}